package edu.ntu.im.p2ptrade.msg;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPOutputStream;

import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.ByteArrayMessageElement;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import edu.ntu.im.p2ptrade.OwnerManager;
import edu.ntu.im.p2ptrade.P2PTrade;
import edu.ntu.im.p2ptrade.entity.User;

public class UserInfo implements PMessage {
	public static String PUBLIC_KEY = "public_key";
	User user;

	public UserInfo(User user) {
		this.user = user;
	}

	public Message getMessageToTransfer() throws Exception {
		Message msg = new Message();
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(USERNAME, P2PTrade.getInstance()
						.getPeerName(), null));
		if (OwnerManager.getOwner().getDesc() == null
				|| "".equals(OwnerManager.getOwner().getDesc())) {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(USERDESC, DUMMY, null));
		}else {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(USERDESC, OwnerManager.getOwner().getDesc(), null));
		}
		if (OwnerManager.getOwner().getNickName() == null
				|| "".equals(OwnerManager.getOwner().getNickName())) {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(UNICKNAME, DUMMY, null));
		}else {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(UNICKNAME, OwnerManager.getOwner().getNickName(), null));
		}
		if (OwnerManager.getOwner().getPhotoURL() == null
				|| "".equals(OwnerManager.getOwner().getPhotoURL())) {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
					new StringMessageElement(UPICURL, DUMMY, null));
		}else {
			msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(UPICURL, OwnerManager.getOwner().getPhotoURL(), null));
		}
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(UTRUST, String.valueOf(OwnerManager.getOwner().isTrustable()), null));
		msg.addMessageElement(P2PTrade.getInstance().getAppName(),
				new StringMessageElement(TYPE, UserInfo.class.getSimpleName(),
						null));
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(user.getPublicKey());
			oos.close();
			bos.close();
			addByteArrayToMessage(msg, P2PTrade.getInstance().getAppName(),
					PUBLIC_KEY, bos.toByteArray(), true);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}

		return msg;
	}

	public static void addByteArrayToMessage(Message message, String nameSpace,
			String elemName, byte[] data, boolean compress) throws IOException {
		byte[] buffer = data;
		MimeMediaType mimeType = MimeMediaType.AOS;
		message.addMessageElement(nameSpace, new ByteArrayMessageElement(
				elemName, mimeType, buffer, null));
	}

	public String toLogMessage() {
		// TODO Auto-generated method stub
		return null;
	}

}
